package com.tianqicode.demo.processor;

import com.tianqicode.demo.config.MySqlDatasource;
import com.tianqicode.spring.beans.BeansException;
import com.tianqicode.spring.beans.factory.config.BeanPostProcessor;

public class DefaultDataSourceBeanPostProcessor implements BeanPostProcessor {

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof MySqlDatasource) {
            if (((MySqlDatasource) bean).getPort() == null) {
                Integer defaultPort = 3306;
                ((MySqlDatasource) bean).setPort(defaultPort);
            }
        }
        return bean;
    }
}
